package com.eclink.asset.workflow.controller;

import com.eclink.asset.asset.domain.dao.FlowApply;
import com.eclink.asset.asset.domain.query.AssetApplySaveQuery;
import com.eclink.asset.domain.dto.FlowApplyDetailDTO;
import com.eclink.asset.pojo.query.ApplyQuery;
import com.eclink.asset.workflow.domain.dao.FlowTask;
import com.eclink.asset.workflow.service.ApplyService;
import com.eclink.asset.workflow.service.FlowService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 申请接口
 *
 * @author zcc
 * @date 2024-06-04
 */
@Api(tags ="申请接口")
@RestController
@RequestMapping("/apply")
public class ApplyController extends BaseController {

    @Resource
    private ApplyService service;
    @Autowired
    private FlowService flowService;

    @ApiOperation("查询申请列表")
    @PostMapping("/list")
    public ResponseEntity<Page<FlowApply>> list(@RequestBody ApplyQuery query, Pageable page) {
        String applyType = query.getApplyType();
        query.setApplyType(applyType);
        query.setUserId(SecurityUtils.getUserId());
        List<FlowApply> list = service.selectList(query, page);

        long total = ((com.github.pagehelper.Page)list).getTotal();

        return ResponseEntity.ok(new PageImpl<>(list, page, total));
    }

    @ApiOperation("详情")
    @GetMapping("/{id}")
    public ResponseEntity<FlowApplyDetailDTO> detail(@PathVariable String id) {
        FlowApplyDetailDTO detail = service.getDetailById(id);
        return ResponseEntity.ok(detail);
    }

    @ApiOperation("通过单号获取详情")
    @GetMapping("/detailByNumber/{number}")
    public ResponseEntity<FlowApplyDetailDTO> detailByNumber(@PathVariable String number) {
        FlowApplyDetailDTO detail = service.getDetailByNumber(number);
        return ResponseEntity.ok(detail);
    }

    @ApiOperation("删除")
    @DeleteMapping("/{id}")
    public ResponseEntity<Integer> delete(@PathVariable String id) {
        service.delete(id);
        return ResponseEntity.ok(1);
    }

    @ApiOperation("保存")
    @PostMapping("/save")
    public ResponseEntity<Integer> save(@RequestBody AssetApplySaveQuery saveDTO) {
        service.save(saveDTO);
        return ResponseEntity.ok(1);
    }

    @ApiOperation("提交")
    @PostMapping("/sub/{id}")
    public ResponseEntity<Integer> sub(@PathVariable String id) {
        service.sub(id);
        return ResponseEntity.ok(1);
    }

    @ApiOperation("保存")
    @PostMapping("/saveAndSub")
    public ResponseEntity<Integer> saveAndSub(@RequestBody AssetApplySaveQuery saveDTO) {
        service.saveAndSub(saveDTO);
        return ResponseEntity.ok(1);
    }


}
